package branch_access;

import mware_lib.StubSkeletonCom;

public class Manager_Stub extends Manager{
	private String remoteName;
	private String remoteHostName;
	int port;
	
	public Manager_Stub(String remoteName, int port, String remoteHostName) {
		this.port = port;
		this.remoteHostName = remoteHostName;
		this.remoteName = remoteName;
	}

	@Override
	public String createAccount(String owner) {		
		String s = null;
		try {
			Object[] params  = {owner};
			//contact the skeleton to execute operation
			s = (String)StubSkeletonCom.methodOfObject(this.getClass().getMethod("createAccount", String.class), remoteName, params, remoteHostName, port);
			
//			if(s != null){
//				String[] splitted = s.split(":");
//				if(splitted[0].equals("InvocationTargetException")){
//					throw new Exception(splitted[1]);
//				}
//			}
			
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (Exception e) {
			System.err.println("Exception in Skeleton: "+e);	
//			return null;
		}
		StubSkeletonCom.handleIfException(s);
		return s;
	}

	@Override
	public boolean removeAccount(String accountID) {
		boolean b = false;
		String s = null;
		try {
			Object[] params  = {accountID};
			//contact the skeleton to execute operation
			Object o= StubSkeletonCom.methodOfObject(this.getClass().getMethod("removeAccount", String.class), remoteName, params, remoteHostName, port);
			if(o == null){
				return false;
			}else if(o instanceof String){
				s = (String) o;
			}else{
				b = (Boolean) o;
			}
			//additionally send internal command to unbind the name
			StubSkeletonCom.sendIt("unbind:"+accountID, false, remoteHostName, port);
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (Exception e) {
			System.err.println("Exception in Skeleton: "+e);
		}
		StubSkeletonCom.handleIfException(s);
		return b;
	}

}
